package com.share.cloudStorage.recycle.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.share.cloudStorage.files.model.Files;
import com.share.cloudStorage.files.service.FilesService;
import com.share.cloudStorage.filter.SecurityFilter;
import com.share.cloudStorage.recycle.model.Recycle;
import com.share.cloudStorage.recycle.service.RecycleService;
import com.share.cloudStorage.shares.model.Shares;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1/recycle")
public class RecycleController {

    @Autowired
    private RecycleService recycleService;

    @Autowired
    private FilesService filesService;

    //获取当前用户的回收站信息
    @GetMapping("getRecycle")
    public List<Recycle> getRecycle(){
        return recycleService.getRecycleInfo(SecurityFilter.userid.get());
    }

    //删除回收站文件
    @GetMapping("delFiles")
    @Transactional
    public void  deleteRecycle(@RequestParam("recycleIds") String recycleIds,@RequestParam("fileIds") String fileIds){
        //将回收站表对应回收id进行逻辑删除
        UpdateWrapper<Recycle> updateWrapper = new UpdateWrapper<>();
        String[] split= recycleIds.split(",");
        List<Integer> recycleIdList = Arrays.stream(split)
                .map(Integer::parseInt) // 将字符串转换为整数
                .collect(Collectors.toList());
        updateWrapper.in("recycle_id", recycleIdList).set("is_restored",1);
        recycleService.update(updateWrapper);

        //对文件表进行逻辑删除
        String[] fileSplit= fileIds.split(",");
        int[] array = new int[fileSplit.length];
        for (int i = 0; i < fileSplit.length; i++) {
            array[i] = Integer.parseInt(fileSplit[i]);
        }
        filesService.deleteFiles(array);
    }

    //还原回收站
    @GetMapping("restoreFiles")
    @Transactional
    public void restoreFiles(@RequestParam("recycleIds") String recycleIds,@RequestParam("fileIds") String fileIds){
        //将回收站表对应回收id进行逻辑删除
        UpdateWrapper<Recycle> updateWrapper = new UpdateWrapper<>();
        String[] split= recycleIds.split(",");
        List<Integer> recycleIdList = Arrays.stream(split)
                .map(Integer::parseInt) // 将字符串转换为整数
                .collect(Collectors.toList());
        updateWrapper.in("recycle_id", recycleIdList).set("is_restored",1);
        recycleService.update(updateWrapper);

        //对文件表进行还原
        String[] fileSplit= fileIds.split(",");
        List<Integer> fileIdList = Arrays.stream(fileSplit)
                .map(Integer::parseInt) // 将字符串转换为整数
                .collect(Collectors.toList());
        //查询文件以及他的父id
        List<Integer> filesIdList =  filesService.searchFileIdAndParentId(fileIdList);
        filesService.restoreFiles(fileIdList);

        UpdateWrapper<Files> filesUpdateWrapper = new UpdateWrapper<>();
        filesUpdateWrapper.in("file_id", filesIdList).set("is_recycle",0);
        filesService.update(filesUpdateWrapper);
    }

}

